#! /usr/bin/python
# -*- coding: utf-8 -*-
import database
import gushici_url
from gsw_fangyi_shangxi_model import FanyiShangxi
from gsw_yizhushang_model import YiZhuShang
import utlis



def getgushiwen_author(chaodai=''):
    pageIdex = 1
    db = database.openDatabase()
    data = gushici_url.get_author(pageIdex, chaodai)
    while data is not None and len(data) > 0:
        print(pageIdex)
        for value in data:
            print(str(value.obj_2_json()))
            database.insert_author(db, value)
        pageIdex = pageIdex + 1
        data = gushici_url.get_author(pageIdex)
    db.close()


def getgushiwen_data():
    db = database.openDatabase()
    authors_id = database.get_author_id('idnew', db)
    for value in authors_id:
        print(value[0])
        pageIdex = 1
        data = gushici_url.get_sw_by_author(value[0], pageIdex)
        while data is not None and len(data) > 0:
            print(pageIdex)
            for tmp in data:
                print(str(tmp.obj_2_json()))
                database.insert_gushiwen(db, tmp)
            pageIdex = pageIdex + 1
            data = gushici_url.get_sw_by_author(value[0], pageIdex)
    db.close()


def getauthorziliao():
    db = database.openDatabase()
    authors_ids = database.get_author_id('id, idnew', db)
    authors_id = []
    for value in authors_ids:
        if not database.have_author_ziliao_id(db, value[0]):
            authors_id.append(value[1])
    empty_list = []
    for value in authors_id:
        print(value)
        data = gushici_url.get_ziliao_by_author(value)
        if data is not None:
            if len(data) > 0:
                for tmp in data:
                    print(str(tmp.obj_2_json()))
                    database.insert_author_ziliao(db, tmp)
            else:
                empty_list.append(value)
    for value in empty_list:
        print(value)


def getmingju(c='', t=''):
    pageIdex = 1
    db = database.openDatabase()
    data = gushici_url.getmingju(pageIdex, c, t)
    while data is not None and len(data) > 0:
        print(str(pageIdex) + " " + c + "  " + t)
        for value in data:
            value.themeStr = c
            value.classifyStr = t
            print(str(value.obj_2_json()))
            database.insert_mingju(db, value)
        pageIdex = pageIdex + 1
        data = gushici_url.getmingju(pageIdex, c, t)
    db.close()

def getfanyishangxi(id):
    db = database.openDatabase()
    data = gushici_url.getgushiwen(id)
    if data is not None:
        shangxi = data.get('tb_shangxis')
        if shangxi is not None:
            shangxiList = shangxi.get('shangxis')
            if shangxiList is not None and len(shangxiList) > 0:
                for value in shangxiList:
                    database.insert_shangxi(db, FanyiShangxi(value))
        fanyis = data.get('tb_fanyis')
        if fanyis is not None:
            fanyiList = fanyis.get('fanyis')
            if fanyiList is not None and len(fanyiList) > 0:
                for value in fanyiList:
                    database.insert_fanyi(db, FanyiShangxi(value))


def getfanyishangxi_pre():
    db = database.openDatabase()
    errprint = []
    p = 562
    size = 50
    gsw_id = database.get_gushiwen_p(db, 'idnew', p, size)
    while len(gsw_id) > 0:
        print("currentPageIndex = " + str(p))
        for value in gsw_id:
            print(value[0])
            try:
                getfanyishangxi(value[0])
            except:
                errprint.append(value[0])
        p = p + 1
        gsw_id = database.get_gushiwen_p(db, 'idnew', p, size)
    f = open("/Users/tanzx/Desktop/out.txt", "w")
    for value in errprint:
        text = str(value) + '\n'
        f.write(text)
    f.close()
    db.close()


def test():
    db = database.openDatabase()
    f = open("/Users/tanzx/Desktop/no_ziliao_author_ids", "r")
    lines = f.readlines()
    errorlist = []
    for line in lines:
        value = line.strip('\n')
        print(value)
        data = gushici_url.get_ziliao_by_author(value)
        while data is not None and len(data) > 0:
            if data is not None:
                if len(data) > 0:
                    for tmp in data:
                        print(str(tmp.obj_2_json()))
                        database.insert_author_ziliao(db, tmp)
                else:
                    errorlist.append(value[0])
    f.close()
    db.close()
    for value in errorlist:
        print(value)


def getgswpic():
    db = database.openDatabase()
    data = database.get_author_id('id, pic', db)
    emptylist = []
    for value in data:
        pic = value[1].strip()
        if len(pic) > 0:
            gushici_url.save_img(pic)
        else:
            emptylist.append(value[0])
    f = open("/Users/tanzx/Desktop/pic.txt", "w")
    for value in emptylist:
        text = str(value) + '\n'
        f.write(text)
    f.close()


def getgswyizhushang():
    db = database.openDatabase()
    errprint = []
    p = 0
    size = 50
    gsw_id = database.get_gushiwen_p(db, 'id', p, size)
    while len(gsw_id) > 0:
        print("currentPageIndex = " + str(p))
        for value in gsw_id:
            try:
                yidata = gushici_url.getyizhushang(value[0], 'yi')
                database.insert_gswyi(db, YiZhuShang(yidata))
                zhudata = gushici_url.getyizhushang(value[0], 'zhu')
                database.insert_gswzhu(db, YiZhuShang(zhudata))
                yizhudata = gushici_url.getyizhushang(value[0], 'yizhu')
                database.insert_gswyizhu(db, YiZhuShang(yizhudata))
                shangdata = gushici_url.getyizhushang(value[0], 'shang')
                database.insert_gswshang(db, YiZhuShang(shangdata))
            except:
                errprint.append(value[0])
        p = p + 1
        gsw_id = database.get_gushiwen_p(db, 'id', p, size)
    f = open("/Users/tanzx/Desktop/yizhushang.txt", "w")
    for value in errprint:
        text = str(value) + '\n'
        f.write(text)
    f.close()
    db.close()


if __name__ == '__main__':
    f = open("../tmp/gushici-find/author_langsong.txt", "r")
    # db = database.openDatabase()
    # data = database.get_gushiwen_where('id, langsongAuthorPY', ''' langsongAuthor != '' ''', db)
    lines = f.readlines()
    count = 0
    for value in lines:
        strs = value.split(' ')
        print(strs[0] + "-" + strs[1])
        count = count + 1
    print(count)
    f.close()
